function tldlg(varargin)
%TLDLG Summary of this function goes here
%
%   TLDLG(varagin) Detailed explanation goes here
%
%   Inputs:
%
%
%   Outputs:
%
%
%   Syntax:
%
%
%   Example:
%
%
%   See also:
%
% 
%   Author(s): Xiaobiao Huang
%   Copyright 2013-2020 The Xiaobiao Studio.
%   $Revision: 1.0.0.0 $  $Date: 05-Jul-2013 19:20:49 $

% ('create', dlgFunction, hBlock);
switch class(varargin{2})
  case 'char'
    action = varargin{1};
    switch action
      case 'create'
        dlgFunction = varargin{2};
        hBlock = varargin{3};
        createDlg(dlgFunction,hBlock);
      case 'update'
    end
    return;

  case 'double'
    pageName = varargin{1};
    dlgfig = varargin{2};
    pageAction = varargin{3};
    uimenuCallback(pageName,dlgfig,pageAction)
    return;
end
end % end of function tldlg

function createDlg(dlgFunction,hBlock)
global dlgdata sheetPanels;
	maskType = get_param(hBlock, 'MaskType'); 
	unsupportBlockList = {'TL_BusInport','TL_BusOutport','TL_DiscreteStateSpace'};
	if any(strcmp(maskType,unsupportBlockList)) 		
		dstabdlg('create', dlgFunction, hBlock);
		return;
  end

dlgFunction = [strrep(maskType, '_', '') 'Dlg'];
dlgFunction = strrep(dlgFunction, '-', '');

[dlgInfo, blockdata] = feval(dlgFunction,'GetDlgInfo',hBlock);

scrsz = get(0,'ScreenSize');
defaultColor = get(0,'DefaultUicontrolBackgroundColor');
sheetdims = setSheetDims(maskType); % [width,height]

dlgfig = figure( ...
    'Visible',                            'off', ...
    'NumberTitle',                        'off', ...
    'Menubar',                            'none', ...
    'DefaultUicontrolHorizontalAlign',    'left', ...
    'DefaultUicontrolUnits',              'pixels', ...
    'Colormap',                           [], ...
    'Name',                               dlgInfo.title, ...
    'DeleteFcn',                          @(obj,evt)dialogDelete(gcbf), ...
    'Resize',                             'off', ...    
    'IntegerHandle',                      'off', ...
    'Color',                              defaultColor, ...
    'Position',                           [scrsz(3)/4 scrsz(3)/8 sheetdims(1)+20 sheetdims(2)+145]);

commonGeom = DSGetCommonGeometry;
textExtent = commonGeom.textExtent; 

nPages = length(dlgInfo.pageNames);
widths(nPages) = 0;

for i=1:nPages,
  set(textExtent, 'String', dlgInfo.pageNames{i});
  ext = get(textExtent, 'Extent');
  widths(i) = ext(3) + 6 + 8;
end

tabdims = {widths,22}; %{width,height}

offsets =[5 35 15 35]; %[sideEdge topEdge sideEdge bottomEdge]
defaultPageNum = 1;

callback = @changTabCallback;
[dlgfig, sheetPos, sheetPanels, buttonPanel] = ...
   tabdlg('create', ...
          dlgInfo.pageNames, ...
          tabdims, ...
          callback, ...
          sheetdims, ...
          offsets, ...
          defaultPageNum, ...
          {}, ...
          dlgfig);
% commonGeom.sheetPos = [5 65 650 365];
commonGeom.sheetPos = sheetPos;
sysbuttons = struct('children',{''});
sysbuttons = FcnCreateSystemButtons(dlgfig, sysbuttons, commonGeom);

dlgdata = struct('options',         tl_options, ...
                'pageNames',        {dlgInfo.pageNames}, ...
                'data',             blockdata, ...
                'olddata',          blockdata, ...
                'CommonGeom',       commonGeom, ...
                'SystemButtons',    sysbuttons.children, ...
                'bIsFullVs',        true, ...
                'maskType',         maskType, ...
                'dlgFunc',          dlgFunction, ...
                'bIsSFOrEMLObject', false, ...
                'bDSDDMan',         true, ...
                'bRefreshTool',     false, ...
                'model',            hBlock, ...
                'toolfig',          false, ...
                'bLocked',          true);

if isfield(dlgInfo,'extdata')
  dlgdata.extdata = dlgInfo.extdata;
end

for iPage = 1 : nPages       
  [sheetDims, ctrlPos] = feval(dlgFunction,'CtrlPositions', ...
                        dlgInfo.pageNames{iPage},commonGeom,dlgdata,sheetdims);                      
  dlgdata.pageData{iPage} = ...
        struct('ctrlPos',ctrlPos,'children','','sheetDims',sheetDims); 
end
iPage = 1;
dlgdata = feval(dlgFunction,'Create', ...
                        dlgInfo.pageNames{iPage},sheetPanels(1,iPage),dlgdata);

dlgdata = feval(dlgFunction,'Update', ...
                      sheetPanels(iPage),dlgdata,iPage);

sheetPanels(2,1) = true;

% set ok button callback
set(sysbuttons.children.OK,'Callback',@(action,evt)buttoncallback('OK'));

% set cancel button callback
set(sysbuttons.children.Cancel,'Callback',@dialogDelete);

% set help button callback
set(sysbuttons.children.Help,'Callback',@(action,evt)buttoncallback('Help'));

% set apply button callback
set(sysbuttons.children.Apply,'Callback',@(action,evt)buttoncallback('Apply'));


set(dlgfig, 'Visible', 'on')
end % end of function  tldlg

%----------------------------------------------------------------
function changTabCallback(varargin)
  global dlgdata sheetPanels;
  iPage = varargin{3};
  pageNames = dlgdata.pageNames;
  dlgFunction = dlgdata.dlgFunc;
  if ~sheetPanels(2,iPage) & iPage ~= 1
    dlgdata = feval(dlgFunction,'Create', ...
                          pageNames{iPage},sheetPanels(1,iPage),dlgdata);

    dlgdata = feval(dlgFunction,'Update', ...
                        sheetPanels(1,iPage),dlgdata,iPage);
    sheetPanels(2,iPage) = true;
  end
end

%----------------------------------------------------------------
function dialogDelete(hObject, eventStruct) %#ok
% Callback from figure window
% Called when the figure is closed or deleted

delete(gcbf);

end

%--------------------------------------------------------------------------
function buttoncallback(action,eventobj)
  global dlgdata;
  
  % pressed help button, show the help
  if strcmp(action,'Help') 
    if exist('tl_online_help','file') == 2
      tl_online_help('Topic',dlgdata.maskType);
    else
      web(['jar:file:///',matlabroot,'/help/toolbox/simulink/help.jar!/slref/' lower(get_param(gcbh,'Blocktype')) '.html'], '-helpbrowser');
   end
   return;
  end
  
  % pressed ok or apply button
  % check what is different
  dlgFunction = dlgdata.dlgFunc;
  bClose = false;
  if strcmp(action,'OK')
    bClose = true;
  end
  [dlgdata,bClose] = feval(dlgFunction,'Apply','',dlgdata,bClose);
  dlgdata.olddata = dlgdata.data;
  set(dlgdata.SystemButtons.Apply,'Enable','off');
  if strcmp(action,'OK')
    dialogDelete('', '');
  end
end
%--------------------------------------------------------------------------

function uimenuCallback(pageName,dlgfig,pageAction)
global dlgdata sheetPanels;

set(dlgdata.SystemButtons.Apply,'Enable','on');

dlgFunction = dlgdata.dlgFunc;
pageNames = dlgdata.pageNames;

iPage = strmatch(pageName,pageNames);
dlgfig = sheetPanels(1,iPage);

dlgdata = feval(dlgFunction,'Manage', ...
                          pageNames{iPage},dlgfig,dlgdata,pageAction);
                      
dlgdata = feval(dlgFunction,'Update', ...
                      dlgfig,dlgdata,iPage);

end % end of function uimenucallback

%--------------------------------------------------------------------------
function sheetdims = setSheetDims(maskType)

sheetdims = [655 365];
maskType = strrep(maskType,'-','_');

sheet.TL_Inport                 = [655 325];
sheet.TL_Outport                = [655 325];
sheet.TL_Constant               = [655 365];
sheet.TL_Sum                    = [655 345];
sheet.TL_Gain                   = [655 370];
sheet.TL_Product                = [650 345];
sheet.TL_LogicalOperator        = [655 330];
sheet.TL_RelationalOperator     = [655 330];
sheet.TL_Fcn                    = [655 340];
sheet.TL_Lookup1D               = [655 370];
sheet.TL_Lookup2D               = [655 370];
sheet.TL_LookupNDDirect         = [655 370];
sheet.TL_PreLookup              = [655 365];
sheet.TL_Interpolation_n_D      = [655 370];
sheet.TL_Assignment             = [655 370];
sheet.TL_Saturate               = [655 420];
sheet.TL_Assignment             = [655 370];
sheet.TL_MinMax                 = [655 345];
sheet.TL_Abs                    = [655 275];
sheet.TL_Signum                 = [655 260];
sheet.TL_RateLimiter            = [655 420];
sheet.TL_Relay                  = [655 420];
sheet.TL_Trigonometry           = [655 345];
sheet.TL_Math                   = [655 345];
sheet.TL_Sqrt                   = [655 345];
sheet.TL_UnitDelay              = [655 345];
sheet.TL_DiscreteTransferFcn    = [655 400];
sheet.TL_DiscreteFilter         = [655 400];
sheet.TL_FIRFilter              = [655 370];
sheet.TL_DiscreteIntegrator     = [655 395];
sheet.TL_DiscreteStateSpace     = [655 450];
sheet.TL_DataStoreWrite         = [520 90];
sheet.TL_DataStoreMemory        = [655 370];
sheet.TL_DataStoreRead          = [570 175];
sheet.TL_CustomCode             = [655 450];
sheet.TL_Merge                  = [655 365];
sheet.TL_Sink                   = [570 175];
sheet.TL_BitClear               = [550 300];
sheet.TL_BitSet                 = [550 300];
sheet.TL_BitwiseOperator        = [545 230];
sheet.TL_ExtractBits            = [655 345];
sheet.TL_ArithShift             = [550 385];
sheet.TL_BusInport              = [875 405];
sheet.TL_BusOutport             = [875 405];
sheet.TL_Switch                 = [655 365];
sheet.TL_MultiPortSwitch        = [655 415];
sheet.TL_UnitDelayRE            = [655 345];
sheet.TL_Rescaler               = [655 275];
try
  sheetdims = sheet.(maskType);
end
end 
